//
//  FloatingView.m
//  FloatingViewByFicow
//
//  Created by ficow on 1/8/16.
//  Copyright © 2016年 ficow. All rights reserved.
//

#import "FloatingView.h"

#define Screen_Width [UIScreen mainScreen].bounds.size.width
#define Screen_Height [UIScreen mainScreen].bounds.size.height
#define Screen_Middle_X Screen_Width / 2
#define Self_Width_Middle self.bounds.size.width / 2
#define Self_Height_Middle self.bounds.size.height / 2
#define StatusBar_Height (20.0f + Self_Height_Middle)


@interface FloatingView()<CAAnimationDelegate>

@property (nonatomic, strong) NSArray *colorArr;

@end

@implementation FloatingView{
    CGPoint currentLocation;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self configView];
    }
    return self;
}

- (void)configView{
    
    //self.layer.masksToBounds = YES;
    //self.layer.cornerRadius = Self_Width_Middle;
    
    self.backgroundColor = [UIColor darkGrayColor];
    _colorArr = @[[UIColor greenColor],[UIColor redColor],[UIColor blueColor],[UIColor yellowColor],[UIColor orangeColor]];
    
    UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGestureHandler:)];
    [self addGestureRecognizer:recognizer];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    self.backgroundColor = [UIColor lightGrayColor];
}


- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = touches.anyObject;
    CGPoint location = [touch locationInView:[self superview]];
    
    //self.backgroundColor = (UIColor*)_colorArr[arc4random() % 5];
    
    if (location.x < Self_Width_Middle) {
        location.x = Self_Width_Middle;
    }
    if (location.x > (Screen_Width - Self_Width_Middle)) {
        location.x = (Screen_Width - Self_Width_Middle);
    }
    if (location.y < StatusBar_Height) {
        location.y = StatusBar_Height;
    }
    if (location.y > (Screen_Height - Self_Height_Middle)) {
        location.y = (Screen_Height - Self_Height_Middle);
    }
    self.center = location;
    currentLocation = location;
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self finishTouchingTask];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self finishTouchingTask];
}

- (void)finishTouchingTask{
    self.backgroundColor = [UIColor darkGrayColor];
    BOOL toLeft = NO;
    if (currentLocation.x < Screen_Middle_X) {
        currentLocation.x = 0 + Self_Width_Middle;
        toLeft = YES;
    }else{
        currentLocation.x = Screen_Width - Self_Width_Middle;
    }
    if (_finishMoveBlock) {
        _finishMoveBlock(toLeft);
    }
//    [UIView animateWithDuration:0.3f animations:^{
//        self.center = currentLocation;
//    } completion:^(BOOL finished){
//        [self shakeSelfHorizontallyToLeft];
//    }];
}

- (void)longPressGestureHandler:(UIGestureRecognizer *)recognizer {
    if(recognizer.state == UIGestureRecognizerStateBegan){
        self.backgroundColor = [UIColor blackColor];
        [self shakeSelfHorizontallyToRight];
    }
}

- (void)shakeSelfHorizontallyToLeft{
//    CGPoint center = self.center;
//    CALayer *lbl = [self layer];
//    CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"position"];
//    animation.delegate = self;
//    [animation setValue:@"toViewValue" forKey:@"toViewKey"];
//    [animation setTimingFunction:[CAMediaTimingFunction
//                                  functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
//    CGFloat shakingDistance = 10;
//    center.x += shakingDistance;
//    [animation setFromValue:[NSValue valueWithCGPoint:center]];
//    center.x -= shakingDistance;
//    [animation setToValue:[NSValue valueWithCGPoint:center]];
//    [animation setAutoreverses:YES];
//    [animation setDuration:0.2];
//    [animation setRepeatCount:1];
//    [lbl addAnimation:animation forKey:nil];
}
- (void)shakeSelfHorizontallyToRight{
    CGPoint center = self.center;
    CALayer *lbl = [self layer];
    CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"position"];
    animation.delegate = self;
    [animation setValue:@"toViewValue" forKey:@"toViewKey"];
    [animation setTimingFunction:[CAMediaTimingFunction
                                  functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    CGFloat shakingDistance = 10;
    center.x += shakingDistance;
    [animation setFromValue:[NSValue valueWithCGPoint:center]];
    center.x -= shakingDistance;
    [animation setToValue:[NSValue valueWithCGPoint:center]];
    [animation setAutoreverses:YES];
    [animation setDuration:0.2];
    [animation setRepeatCount:1];
    [lbl addAnimation:animation forKey:nil];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/



@end
